package com.hd.controller.personaloffice;

import com.hd.controller.base.BaseController;
import com.hd.entity.personaloffice.WorkPlan;
import com.hd.service.WorkCcService;
import com.hd.service.personaloffice.WorkPlanService;
import com.hd.util.StringUtils;
import com.hd.util.TransformCharacter;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
* 工作计划
*/
@RequestMapping("workPlan")
@Controller
public class WorkPlanController extends BaseController {
    @Autowired
    private WorkPlanService workPlanService;
    @Autowired
    private WorkCcService workCcService;
    /**
     * 主页
     * @return
     */
    @RequestMapping("manager")
    public String manager() {
        return "jsp/personaloffice/workPlan/workPlanList";
    }
    /**
     * 列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/dataGrid",method=RequestMethod.POST)
    @ResponseBody
    public Object dataGrid(WorkPlan workPlan, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        pageInfo.setCondition(condition);
        workPlanService.selectDataGrid(pageInfo,workPlan);
        return pageInfo;
    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "jsp/personaloffice/workPlan/workPlanAdd";
    }

    /**
     * 添加
     * @param workPlan
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(WorkPlan workPlan) {
        if(StringUtils.isNotNull(workPlan.getContent())){
            workPlan.setContent(TransformCharacter.doUntransition(workPlan.getContent()));
        }
        workPlanService.save(workPlan);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable("id") String id) {
        workPlanService.delete(id);
        return renderSuccess("删除成功！");
    }

    /**
     * 作废
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/cancel",method = RequestMethod.POST)
    @ResponseBody
    public Object cancel(String id,Integer status) {
        status=-1;
        workPlanService.updateStatus(id,status);
        return renderSuccess("删除成功！");
    }

    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage/{id}")
	public String editPage(Model model, @PathVariable("id")String id) {
	 WorkPlan workPlan = workPlanService.get(id);
        model.addAttribute("workPlan", workPlan);
        return "jsp/personaloffice/workPlan/workPlanEdit";
    }

    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/viewPage/{id}")
    public String viewPage(Model model, @PathVariable("id") String id) {
        WorkPlan workPlan = workPlanService.get(id);
        model.addAttribute("workPlan", workPlan);
        return "jsp/personaloffice/workPlan/workPlanView";
    }

    /**
     * 编辑
     * @param workPlan
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(WorkPlan workPlan) {
        if(StringUtils.isNotNull(workPlan.getContent())){
            workPlan.setContent(TransformCharacter.doUntransition(workPlan.getContent()));
        }
        workPlanService.update(workPlan);
        return renderSuccess("编辑成功！");
    }

    /**
     * 通过年月查询记录
     * @param param 格式yyyy-MM
     * @return
     */
    @ResponseBody
    @RequestMapping("/getByMonth")
    public Object getbyMonth(String param,String userid){
        try{
            List<WorkPlan> workPlanList=workPlanService.getByMonth(param,userid);
            List<Date>saveDates=new ArrayList<>();
            for(int i=0;i<workPlanList.size();i++){
                saveDates.add(workPlanList.get(i).getSaveDate());
            }
            return renderSuccess(saveDates);
        }catch (Exception e){
            e.printStackTrace();
            return renderError("出错误了");
        }
    }

    /**
     * 通过保存日期查询
     * @param saveDate
     * @return
     */
    @ResponseBody
    @RequestMapping("/getBySaveDate")
    public Object getBySaveDate(Date saveDate,String userid){
        return workPlanService.getBySaveDate(saveDate, userid);
    }

    /**
     * 模式二显示
     * @return
     */
    @RequestMapping("/manager2")
    public String manager2(){
        return "jsp/personaloffice/workPlan/workPlanList2";
    }

    /**
     * 跳转到待办审批的页面
     * @param id
     * @return
     */
    @RequestMapping("/todo/{id}")
    public String todo(@PathVariable("id") String id,Model model){
        WorkPlan workPlan=workPlanService.get(id);
        System.out.println(workPlan);
        model.addAttribute("workPlan",workPlan);
        return "jsp/personaloffice/workPlan/workPlanTodo";
    }


}
